package com.mapfinal.resource.postgis;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbPro;
import com.lambkit.db.DbManager;
import com.lambkit.db.datasource.DataSourceConfig;
import com.mapfinal.kit.StringKit;
import com.mapfinal.resource.DataStore;
import com.mapfinal.resource.DataStoreType;
import com.mapfinal.resource.Resource;

/**
 * postgis的数据存储
 * @author yangyong
 *
 */
public class PostgisDataStore implements DataStore {

	private DataSourceConfig config;
	private boolean isStart = false;

	private String id;
	private String name;
	
	private String url;
    private String user;
    private String password;

    private String dbname;
    private String schema;
    
	public PostgisDataStore(String name, String url, String user, String password, String schame) {
		this.id = StringKit.uuid();
		this.name = name;
		if(StringKit.isNotBlank(name) && StringKit.isNotBlank(url) && StringKit.isNotBlank(user) && StringKit.isNotBlank(password)) {
			config = new DataSourceConfig();
			config.setName(name + "_" + getId());
			config.setUrl(url);
			config.setUser(user);
			config.setPassword(password);
			config.setType(DataSourceConfig.TYPE_POSTGRESQL);
			config.setDriverClassName("org.postgresql.Driver");
			config.setSchema(StrKit.isBlank(schame) ? "public" : schame);
			config.setFactory("com.lambkit.db.datasource.DruidDataSourceFactory");
		}
	}
	
	public DbPro db() {
		return Db.use(config.getName());
	}
	
	public boolean isStart() {
		return isStart;
	}

	@Override
	public void start() {
		// TODO Auto-generated method stub
		if(config==null || !config.isConfigOk() || StrKit.isBlank(config.getName())) return;
		DbManager.me().start(config);
		isStart = true;
	}

	@Override
	public void stop() {
		// TODO Auto-generated method stub
		if(config==null || StrKit.isBlank(config.getName())) return;
		DbManager.me().stop(config);
		isStart = false;
	}
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}
	
	@Override
	public Resource getResource(String name) {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public DataStoreType getType() {
		// TODO Auto-generated method stub
		return DataStoreType.PostGIS;
	}

	@Override
	public String getId() {
		// TODO Auto-generated method stub
		return id;
	}

	@Override
	public String getName() {
		// TODO Auto-generated method stub
		return name;
	}

	@Override
	public String getUrl() {
		// TODO Auto-generated method stub
		return url;
	}
	
	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getDbname() {
		return dbname;
	}

	public void setDbname(String dbname) {
		this.dbname = dbname;
	}

	public String getSchema() {
		return schema;
	}

	public void setSchema(String schema) {
		this.schema = schema;
	}

	public void setId(String id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setUrl(String url) {
		this.url = url;
	}
}
